package com.intellagric.service.impl.module.sys_user;

import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.SysUserRoleMapper;
import com.intellagric.pojo.SysUserRoleExample;
import com.intellagric.pojo.SysUserRoleKey;
import com.intellagric.service.module.sys_user.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/13
 * @DESC： 用户角色id信息服务实现
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 添加用户角色id信息
     * @param sysUserRoleKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addUserRole(SysUserRoleKey sysUserRoleKey) {
        if (sysUserRoleMapper.insertSelective(sysUserRoleKey) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除用户角色id信息
     * @param userId
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteUserRoleByUserId(String userId) {
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(userId);
        if (sysUserRoleMapper.deleteByExample(sysUserRoleExample) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改用户角色id信息
     * @param sysUserRoleKey
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editUserRole(SysUserRoleKey sysUserRoleKey) {
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(sysUserRoleKey.getRoleId());
        if ( sysUserRoleMapper.updateByExample(sysUserRoleKey,sysUserRoleExample) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改用户角色id信息
     * @param userId
     * @return ResponseMessage
     */
    @Override
    public SysUserRoleKey getUserRoleById(String userId) {
        SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        sysUserRoleExample.createCriteria().andUserIdEqualTo(userId);
        List<SysUserRoleKey> sysUserRoleKeys = sysUserRoleMapper.selectByExample(sysUserRoleExample);
        return sysUserRoleKeys.get(0);
    }
}
